﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BOMS.DBUtility;//请先添加引用
namespace BOMS.DAL
{
    /// <summary>
    /// 数据访问类WarningDealwith。
    /// </summary>
    public class WarningDealwith
    {
        public WarningDealwith()
        { }
        #region  成员方法      

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BOMS.Model.WarningDealwith model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("BEGIN TRANSACTION;");
            strSql.Append("BEGIN TRY;");
            strSql.Append("update dbo.S_Warning set IsActive=0 where ID=@WarningID; insert into WarningDealwith(");
            strSql.Append("WarningID,Operator,OperateDescr,CreateTime,CreatorID)");
            strSql.Append(" values (");
            strSql.Append("@WarningID,@Operator,@OperateDescr,@CreateTime,@CreatorID)");
            strSql.Append(";select @@IDENTITY;");
            strSql.Append("END TRY BEGIN CATCH IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;");
            strSql.Append(" END CATCH;IF @@TRANCOUNT>0 COMMIT TRANSACTION;");
            SqlParameter[] parameters = {
					new SqlParameter("@WarningID", SqlDbType.Int,4),
					new SqlParameter("@Operator", SqlDbType.NVarChar,50),
					new SqlParameter("@OperateDescr", SqlDbType.NVarChar,2000),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@CreatorID", SqlDbType.Int,4)};
            parameters[0].Value = model.WarningID;
            parameters[1].Value = model.Operator;
            parameters[2].Value = model.OperateDescr;
            parameters[3].Value = model.CreateTime;
            parameters[4].Value = model.CreatorID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(BOMS.Model.WarningDealwith model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update WarningDealwith set ");
            strSql.Append("WarningID=@WarningID,");
            strSql.Append("Operator=@Operator,");
            strSql.Append("OperateDescr=@OperateDescr,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("CreatorID=@CreatorID");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@WarningID", SqlDbType.Int,4),
					new SqlParameter("@Operator", SqlDbType.NVarChar,50),
					new SqlParameter("@OperateDescr", SqlDbType.NVarChar,2000),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@CreatorID", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.WarningID;
            parameters[2].Value = model.Operator;
            parameters[3].Value = model.OperateDescr;
            parameters[4].Value = model.CreateTime;
            parameters[5].Value = model.CreatorID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WarningDealwith ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BOMS.Model.WarningDealwith GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,WarningID,Operator,OperateDescr,CreateTime,CreatorID from WarningDealwith ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            BOMS.Model.WarningDealwith model = new BOMS.Model.WarningDealwith();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WarningID"].ToString() != "")
                {
                    model.WarningID = int.Parse(ds.Tables[0].Rows[0]["WarningID"].ToString());
                }
                model.Operator = ds.Tables[0].Rows[0]["Operator"].ToString();
                model.OperateDescr = ds.Tables[0].Rows[0]["OperateDescr"].ToString();
                if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatorID"].ToString() != "")
                {
                    model.CreatorID = int.Parse(ds.Tables[0].Rows[0]["CreatorID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,WarningID,Operator,OperateDescr,CreateTime,CreatorID ");
            strSql.Append(" FROM WarningDealwith ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,WarningID,Operator,OperateDescr,CreateTime,CreatorID ");
            strSql.Append(" FROM WarningDealwith ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "WarningDealwith";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

